from devicetest.api import Asserts

from devicetest.aw.OpenHarmony import CommonOH
from devicetest.core.test_case import TestCase, Step
from devicetest.log.logger import print_info

from aw.common import common_setup, common_teardown
from aw.pom import DeskTop


class PreTest_RK3568_Basic_Desktop_0600(TestCase, DeskTop):
    # 预置条件：
    # 烧写目标版本后，正常进入桌面
    # 测试步骤：
    # 1、长按桌面应用添加到dock栏
    # 2、长按dock栏应用移动到桌面
    # 3、长按应用拖动到本页任意处或下一页任意处
    # 预期结果：
    # 1、应用成功从桌面移动到dock栏
    # 2、应用成功从dock栏移动到桌面
    # 3、可拖动到本页任意位置或下页，无卡顿、黑屏、白屏等现象

    def __init__(self, controllers):
        self.TAG = self.__class__.__name__
        TestCase.__init__(self, self.TAG, controllers)
        # 图标的初始坐标
        self.x0 = 78
        self.y0 = 186

    def setup(self):
        common_setup(self.Phone1)

    def process(self):
        Step('步骤1：长按桌面应用添加到dock栏')
        point_x, point_y = CommonOH.getPointByCondition(self.Phone1, '相机')
        x0, y0 = point_x, point_y - 50
        self.x0 = x0
        self.y0 = y0
        dock_x, dock_y = 628, 1145
        print_info('坐标变化：({}, {}) --> ({}, {})'.format(x0, y0, dock_x, dock_y))
        CommonOH.drag(self.Phone1, sx=x0, sy=y0, ex=dock_x, ey=dock_y)
        Step('预期结果1：相机应用成功从桌面移动到dock栏')
        CommonOH.checkIfTextExist(self.Phone1, '相机', expect=False)

        Step('步骤2：长按dock栏应用移动到桌面')
        print_info('坐标变化：({}, {}) --> ({}, {})'.format(dock_x, dock_y, x0, y0))
        CommonOH.drag(self.Phone1, sx=dock_x, sy=dock_y, ex=x0, ey=y0)
        Step('预期结果2：应用成功从dock栏移动到桌面')
        CommonOH.checkIfTextExist(self.Phone1, '相机')

        Step('步骤3：长按拖动到本页任意处')
        new_x, new_y = x0, y0 + 141 * 3
        print_info('坐标变化：({}, {}) --> ({}, {})'.format(x0, y0, new_x, new_y))
        CommonOH.drag(self.Phone1, sx=x0, sy=y0, ex=new_x, ey=new_y)
        Step('预期结果3：可拖动到本页任意位置')
        point_x, point_y = CommonOH.getPointByCondition(self.Phone1, '相机')
        x1, y1 = point_x, point_y - 50
        print_info('相机应用当前位置：({}, {})'.format(x1, y1))
        # 防止像素变化，允许一点误差
        Asserts.assert_true(abs(x1 - new_x) < 5 and abs(y1 - new_y) < 5)

        Step('步骤4：长按拖动到下页')
        CommonOH.drag(self.Phone1, sx=x1, sy=y1, ex=640, ey=y1)
        CommonOH.drag(self.Phone1, sx=640, sy=y1, ex=690, ey=y1)
        # print_info('滑动到下一页')
        # CommonOH.swipe(self.Phone1, x1=600, y1=800, x2=120, y2=800)
        Step('预期结果4：可拖动到下页')
        CommonOH.checkIfTextExist(self.Phone1, '相机')
        CommonOH.checkIfTextExist(self.Phone1, '音乐', expect=False)

    def teardown(self):
        Step('收尾0：还原桌面布局')
        CommonOH.shell(self.Phone1, 'rm -rf /data/*')
        CommonOH.safeReboot(self.Phone1)
        common_teardown(self.Phone1)
